/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 9/8/12
 * Time: 10:35 PM
 * To change this template use File | Settings | File Templates.
 */
package view
{

    import caurina.transitions.Tweener;

    import flash.display.Sprite;

    import flash.display.Stage;

    import model.types.ApplicationStates;

    import view.states.IState;

    public class StateTransition
    {
        public var zoomInNumbers:Object = null;
        public var zoomOutNumbers:Object = null;

        public var zoomInScreen:Object = null;
        public var zoomOutScreen:Object = null;

        public var slideToRight:Object = null;
        public var slideToLeft:Object = null;
        public var slideToScreen:Object = null;

        private var numbers:Sprite = null;
        private var states:Array = null;

        public function init(stage:Stage, numbers:Sprite, states:Array):void
        {
            var sw:Number = stage.stageWidth || stage.width;
            var sh:Number = stage.stageHeight || stage.height;
            this.numbers = numbers;
            this.states = states;

            zoomOutNumbers = {
                scaleX: 0.5,
                scaleY: 0.5,
                x: sw / 4,
                y: sh / 4,
                _Blur_blurX: 5,
                _Blur_blurY: 5,
                time: Config.ANIMATION_SPEED
            };

            zoomInNumbers  = {
                scaleX: 1,
                scaleY: 1,
                x: 0,
                y: 0,
                _Blur_blurX: 0,
                _Blur_blurY: 0,
                time: Config.ANIMATION_SPEED
            };

            zoomInScreen = {
                alpha: 1.0,
                scaleX: 1,
                scaleY: 1,
                x: 0,
                y: 0,
                time: Config.ANIMATION_SPEED
            };

            zoomOutScreen = {
                x: - sw / 4,
                y: - sh / 4,
                scaleX: 1.5,
                scaleY: 1.5,
                alpha: 0.0,
                time: Config.ANIMATION_SPEED
            };

            slideToLeft = {
                x: -sw,
                time: Config.ANIMATION_SPEED
            };

            slideToRight = {
                x: sw,
                time: Config.ANIMATION_SPEED
            };

            slideToScreen = {
                x: 0,
                alpha: 1.0,
                time: Config.ANIMATION_SPEED
            };
        }

        public function changeState(currentStateId:uint, newStateId:uint):void
        {
            var currentSkin:Sprite = null;
            if (currentStateId in states)
            {
                currentSkin = (states[currentStateId] as IState).skin;
                currentSkin.visible = true;
            }
            else
            {
                currentSkin = null;
            }

            var newSkin:Sprite = (states[newStateId] as IState).skin;
            newSkin.visible = true;

            switch (newStateId)
            {
                case ApplicationStates.GAME_GAME:
                case ApplicationStates.GAME_INITIALIZE:
                {
                    animate(numbers, zoomInNumbers);
                    if (currentStateId == ApplicationStates.GAME_MENU)
                        animate(currentSkin, zoomOutScreen, true);
                    break;
                }

                case ApplicationStates.GAME_MENU:
                {
                    animate(numbers, zoomOutNumbers);
                    if (currentStateId == ApplicationStates.GAME_GAME)
                    {
                        currentSkin.visible = false;
                        prepare(newSkin, zoomOutScreen);
                        animate(newSkin, zoomInScreen);
                    }
                    if (currentStateId == ApplicationStates.GAME_RESULT)
                    {
                        animate(currentSkin, slideToRight, true);
                        prepare(newSkin, zoomInScreen);
                        prepare(newSkin, slideToLeft);
                        animate(newSkin, slideToScreen);
                    }
                    break;
                }

                case ApplicationStates.GAME_RESULT:
                {
                    animate(numbers, zoomOutNumbers);
                    if (currentStateId == ApplicationStates.GAME_GAME)
                    {
                        currentSkin.visible = false;
                        prepare(newSkin, zoomOutScreen);
                        animate(newSkin, zoomInScreen);
                    }
                    if (currentStateId == ApplicationStates.GAME_MENU)
                    {
                        animate(currentSkin, slideToLeft, true);
                        prepare(newSkin, zoomInScreen);
                        prepare(newSkin, slideToRight);
                        animate(newSkin, slideToScreen);
                    }
                    break;
                }
            }
        }

        public function prepare(target:Object, source:Object):void
        {
            for (var key:String in source)
                if (key in target)
                    target[key] = source[key];
        }

        public function animate(target:Object, properties:Object, final:Boolean=false):void
        {
            if (final)
            {
                properties["onComplete"] = function ():void {target.visible = false};
            }
            Tweener.addTween(target, properties);
        }
    }
}
